home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FM Towns: Free Software Collection 6
/
FM Towns Free Software Collection 6.iso
/
t_os
/
catlog
/
source
/
file.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-08
|
16KB
|
606 lines
/*
NIFTYのLOG整理 file.c
by GHH01217 山先
$Header: FILE.Cv 1.2 93/02/12 22:45:08 山先 Exp $
*/
#include "log.h"
#include <dir.h>
#define TEST 1
#undef TEST
/************************************************************************/
/* */
/* file の 入出力の高速化のために */
/* */
/************************************************************************/
static char *sentinel;
static FILE *pool_fpi;
static char *catlog_pool=NULL;
static char *check_sentinel , *check_head , *pool_end;
static int catlog_pool_append_0a_sw;
void insert_char( char *start , int c )
{
if ( start < catlog_pool
|| check_sentinel < start
|| pool_end < start
) {
sprintf(str,"領域外です。%2xを追加できません" , c );
error_bug( str ); return;
};
if ( pool_end <= check_sentinel ) {
sprintf(str,"領域不足です。%2xを追加できません。\n" , c );
error_bug( str ); return;
};
memmove( start + 1 , start , check_sentinel - start );
*start = c;
/********************/
/* ポインタ類の更新 */
/********************/
if ( start < LastStrPtr ) LastStrPtr++;
if ( start < PresentStrPtr ) PresentStrPtr++;
if ( start < NextStrPtr ) NextStrPtr++;
if ( start < check_head ) check_head++;
if ( start < sentinel ) sentinel++;
if ( start < check_sentinel ) check_sentinel++;
if ( start < check_pool_delete_start ) check_pool_delete_start++;
}
static void delete_line( char *start , char *end )
{
int len;
if ( start == end ) return;
if ( start == NULL ) return;
if ( end == NULL ) return;
if ( start < catlog_pool || end < catlog_pool ) {
sprintf(str,"delete_line 管理領域を破壊します<%ld> <%ld>",
start - catlog_pool,
end - catlog_pool
);
error_bug( str ); return;
};
len = end - start;
memmove( start , end , check_sentinel - end );
/********************/
/* ポインタ類の更新 */
/********************/
if ( start < LastStrPtr && LastStrPtr < end ) {
LastStrPtr = start;
} else {
if ( end <= LastStrPtr ) LastStrPtr = LastStrPtr - len;
};
if ( start < PresentStrPtr && PresentStrPtr < end ) {
PresentStrPtr = start;
} else {
if ( end <= PresentStrPtr ) PresentStrPtr = PresentStrPtr - len;
};
if ( start < NextStrPtr && NextStrPtr < end ) {
NextStrPtr = start;
} else {
if ( end <= NextStrPtr ) NextStrPtr = NextStrPtr - len;
};
if ( start < check_head && check_head < end ) {
check_head = start;
} else {
if ( end <= check_head ) check_head = check_head - len;
};
if ( start < sentinel && sentinel < end ) {
sentinel = start;
} else {
if ( end <= sentinel ) sentinel = sentinel - len;
};
if ( start < check_pool_delete_start && check_pool_delete_start < end ) {
check_pool_delete_start = start;
} else {
if ( end <= check_pool_delete_start ) {
check_pool_delete_start = check_pool_delete_start - len;
};
};
/* 空いた領域を 0 で埋める */
/*
for ( p=check_sentinel-len; p<check_sentinel ; p++ ) *p = '\0';
*/
}
static int catlog_fread_char;
static int catlog_fread_char_sw = NO;
static int catlog_fread( char *pool1 )
{
int c,len1,len2,sw;
#ifdef TEST
printf("catlog_fread()だよ ");
#endif
sw = 0;
if ( catlog_fread_char_sw != NO ) {
sw = 1;
*pool1++ = catlog_fread_char;
catlog_fread_char_sw = NO;
};
len1 = check_sentinel - pool1; /* ロードするバイト数 */
if ( len1 < 0 ) {
strcpy(str,"catlog_fread ちゃんと領域を確保してね。" );
error_bug( str ); return( 0 );
};
if ( pool1 < catlog_pool ) {
sprintf(str,"catlog_fread 管理領域を破壊しますよ。<%ld> ",
pool1 - catlog_pool
);
error_bug( str ); return( 0 );
};
/* ロードしたバイト数 */
len2 = fread( pool1 , sizeof(char) , len1 , pool_fpi );
if ( len2 < len1 ) {
c = fgetc( pool_fpi ); /* set EOF flag */
if ( c != EOF ) {
catlog_fread_char = c;
catlog_fread_char_sw = YES;
} else {
catlog_fread_char_sw = NO;
};
};
#ifdef TEST
printf("%dバイトロードしたよ。\n" , len2 , sw );
#endif
sentinel = pool1 + len2;
return ( len2 + sw );
}
static int load_sentinel()
{
int len;
if ( catlog_pool <= check_pool_delete_start ) {
delete_line( catlog_pool , check_pool_delete_start );
};
if ( sentinel == check_sentinel ) {
strcpy(str,"確保した作業領域より発言の方が大きいので、"
"データをロードできません。"
);
error_bug( str ); return( 0 );
};
len = catlog_fread( sentinel );
return( len );
}
static char *set_next_str()
{
int len;
NextStrPtr = check_head;
NextStrLen = 0;
if ( check_head == NULL ) return( PresentStrPtr );
if ( check_head >= sentinel ) {
if ( feof( pool_fpi ) != 0 ) { /* EOF */
NextStrPtr = NULL; NextStrLen = 0;
return( PresentStrPtr );
};
/* 処理済みの行を削除 */
len = load_sentinel();
/* 以下は必要ないが……… */
if ( len == 0 ) { /* EOF */
NextStrLen = check_head - NextStrPtr;
check_head = NULL;
return( PresentStrPtr );
};
};
while ( *check_head != 0x0d ) {
if ( *check_head == 0x1a ) { /* ^Z */
/* 処理済みの行を削除 */
/* 空白にする */
*check_head = ' ';
/*
delete_line( check_head , check_head + 1 );
continue;
*/
};
check_head++;
if ( check_head >= sentinel ) {
if ( feof( pool_fpi ) != 0 ) break; /* EOF */
/* 処理済みの行を削除 */
len = load_sentinel();
/* 以下は必要ないが……… */
if ( len == 0 ) { /* EOF */
NextStrLen = check_head - NextStrPtr;
check_head = NULL;
return( PresentStrPtr );
};
};
};
NextStrLen = check_head - NextStrPtr; /* 0x0d は数えない */
if ( *check_head == 0x0d ) check_head++;
/* 0x0a を追加する */
if ( catlog_pool_append_0a_sw == YES && *check_head != 0x0a ) {
insert_char( check_head , 0x0a );
};
check_head++;
return( PresentStrPtr );
}
/********************/
/* ポインタ群の更新 */
/********************/
char *catlog_fgets()
{
/* ファイルの中での位置 */
if ( NextStrPtr != NULL && PresentStrPtr != NULL ) {
pool_seek_ichi += (NextStrPtr - PresentStrPtr);
};
LastStrLen = PresentStrLen;
LastStrPtr = PresentStrPtr;
PresentStrLen = NextStrLen;
PresentStrPtr = NextStrPtr;
return set_next_str(); /* set next_str */
}
/***************************************************/
/* malloc() を使って catlog_pool[] の領域を確保する */
/***************************************************/
static void pool_malloc( size_t fsize )
{
int sw;
size_t max,min,tmp,tuika;
if ( catlog_pool_append_0a_sw == NO ) { tuika = 0;
} else { tuika = fsize * 2 / 3;
};
if ( ( catlog_pool = (char *)malloc( (fsize + tuika)*sizeof(char) )
) != NULL ) {
check_sentinel = catlog_pool + fsize;
pool_end = check_sentinel + tuika;
return;
};
max = fsize; min = 0; tmp = ( max - min ) / 2 + min;
sw = FALSE;
while ( sw == FALSE ) {
if ( ( catlog_pool = (char *)malloc( (tmp + tuika)*sizeof(char) )
) == NULL ) { max = tmp;
} else { min = tmp; free( catlog_pool );
};
tmp = ( max - min ) / 2 + min;
if ( tmp < 1 ) {
tuika = tuika / 2;
if ( tuika == 0 ) {
strcpy(str,"メモリーが足りません。");
error_bug( str ); return;
};
max = fsize; min = 0;
tmp = ( max - min ) / 2 + min;
};
if ( max == min || max - 1 == min ) sw = TRUE;
};
catlog_pool = (char *)malloc( (tmp + tuika)*sizeof(char) );
check_sentinel = catlog_pool + tmp;
pool_end = check_sentinel + tuika;
}
/************************************/
/* file_name のオープンと領域の確保 */
/************************************/
int initial_check_pool( long fsize , int sw )
{
size_t fsize2;
if ( catlog_pool != NULL ) {
strcpy(str,"initial_check_pool ");
error_bug( str ); return( FALSE );
};
catlog_pool_append_0a_sw = sw;
pool_malloc( fsize );
check_head = catlog_pool;
check_pool_delete_start =
LastStrPtr = PresentStrPtr = NextStrPtr = catlog_pool;
LastStrLen = PresentStrLen = NextStrLen = 0;
pool_seek_ichi = 0; /* ファイルの中での位置 */
if ( ( pool_fpi = fopen( file_name ,"rb" ) ) == NULL ) {
/* 考えられないエラー */
error_open_file( "initial_check_pool" );
free( catlog_pool ); return( FALSE );
};
setbuf( pool_fpi , NULL ); /* NON Buffered mode */
if ( ( fsize2 = catlog_fread(catlog_pool) ) <= 0
) {
sprintf(str,"initial_check_pool()で"
"ファイル<%s>をロードできません。\n",
file_name
);
error_bug( str );
end_check_pool(); return( FALSE );
};
if ( fsize == fsize2 ) fgetc( pool_fpi ); /* set EOF flag */
set_next_str();
return( TRUE );
}
/****************************/
/* PresentStrPtr を削除する */
/****************************/
void delete_present_line()
{
if ( NextStrPtr != NULL ) delete_line( PresentStrPtr , NextStrPtr );
set_next_str(); /* set next_str */
}
/****************************/
/* catlog_poolを解放して終了 */
/****************************/
void end_check_pool()
{
fclose( pool_fpi );
if ( catlog_pool != NULL ) {
free( catlog_pool );
} else {
strcpy(str,"領域を確保していないのに解放しようとしているよ。");
error_bug( str );
};
catlog_pool = NULL;
}
static char crlf[ 3 ] = { 0x0d , 0x0a , '\0' };
size_t catlog_fwrite()
{
size_t len;
if ( PresentStrPtr == NULL ) {
len = fwrite(
check_pool_delete_start ,
sizeof(char) ,
sentinel - check_pool_delete_start ,
catlog_fpo
);
if ( *(sentinel - 1 ) != 0x0a ) catlog_fprintf( "" );
} else {
len = fwrite(
check_pool_delete_start ,
sizeof(char) ,
PresentStrPtr - check_pool_delete_start ,
catlog_fpo
);
if ( *(PresentStrPtr - 1 ) != 0x0a ) catlog_fprintf( "" );
};
return( len );
}
void catlog_fprintf( const char *targ )
{
int len;
len = strlen( targ );
if ( len != 0 ) fwrite( targ , sizeof(char) , len , catlog_fpo );
fwrite( crlf , sizeof(char) , 2 , catlog_fpo );
}
/**************************************/
/* file_name のファイルの大きさを得る */
/**************************************/
size_t file_size()
{
size_t fsize;
FILE *fp;
if ( ( fp = fopen( file_name , "r" ) ) == NULL ) return( 0 );
fclose( fp );
fp = fopen( file_name , "ab" );
fsize = ftell( fp );
fclose( fp );
return( fsize );
}
/************************************************************************/
/* */
/* file 名の処理 */
/* */
/************************************************************************/
/*********************************/
/* file の 空白を 0 に置き換える */
/*********************************/
void file_name_space_0( char *file )
{
while ( *file ) {
if ( *file == ' ' ) *file = '0';
file++;
};
}
/************************************************************************/
/* */
/* tmp file の処理 */
/* */
/************************************************************************/
/*******************/
/* 巨大な tmp file */
/*******************/
void set_tmp0_file_name( char *file )
{
sprintf( file , "%s%s" , tmp_path , TMP_FILE_NAME );
}
/*********************/
/* 大もとの tmp file */
/*********************/
static void tmp0_file_error( char *str )
{
printf("\n%s()で<%s>をオープンできません。",str,file_name);
printf("\n\n\tドライブをきちんと設定していますか?");
printf("\n\n\t架空のドライブやCDを指定していたり、");
printf( "\n\t残り容量の無いドライブを指定していませんか?");
printf("\n\nそれでもおかしいというのであれば");
error_bug( "原因不明" );
}
void open_out_tmp0_file()
{
set_tmp0_file_name( file_name );
if ( ( catlog_fpo = fopen( file_name , "wb" )) == NULL ) {
tmp0_file_error("open_out_tmp0_file");
};
/* NON Buffered mode */
/*
setbuf( catlog_fpo , NULL );
*/
}
void open_append_tmp0_file()
{
set_tmp0_file_name( file_name );
if ( ( catlog_fpo = fopen( file_name , "ab" )) == NULL ) {
tmp0_file_error("open_append_tmp0_file");
};
/* NON Buffered mode */
/*
setbuf( catlog_fpo , NULL );
*/
}
void open_read_tmp0_file()
{
set_tmp0_file_name( file_name );
if ( ( catlog_fpi = fopen( file_name , "r" )) == NULL ) {
tmp0_file_error("open_read_tmp0_file");
};
}
void delete_tmp0_file()
{
set_tmp0_file_name( file_name );
remove( file_name );
}
int CATLOG_tmp_file( const char *file )
{
if ( strcmp( file , TMP_FILE_NAME ) == 0 ) return( YES );
if ( strcmp( file , IDX_FILE_NAME ) == 0 ) return( YES );
if ( strcmp( file , CATLOG_TIME_FILE_NAME ) == 0 ) return( YES );
if ( strcmp( file , DONE_FILE_NAME ) == 0 ) return( YES );
return( NO );
}
/*****************************************/
/* kaigishitsu_number を設定しておくこと */
/*****************************************/
void set_index_file_name( char *new_forum )
{
strcpy( file_name , output_path );
if ( Index_mode == 1 ) {
check_and_make_output_path( file_name );
strcat( file_name , IDX_FILE_NAME ); return;
};
switch( kaigishitsu_number ) {
case HP:
strcat( file_name , "HP\\" );
strcat( file_name , new_forum ); break;
case CLIP:
strcat( file_name , "CLIP\\" ); break;
case PATIO:
strcat( file_name , "PATIO\\" );
strcat( file_name , new_forum ); break;
case MAIL:
case ALL_MAIL:
strcat( file_name , "NIFMAIL\\" ); break;
case BILL:
strcat( file_name , "BILL\\" ); break;
case COLLECT_ID:
strcat( file_name , "COLLECT\\" ); break;
default:
strcat( file_name , new_forum ); break;
};
check_and_make_output_path( file_name );
strcat( file_name , "\\" );
strcat( file_name , IDX_FILE_NAME );
}
/************************************************************************/
/* */
/* ソートしたデータを記録する */
/* */
/************************************************************************/
/******************/
/* 各発言のファイル名 */
/******************/
void set_file_name( int count )
{
char file[ 32 ];
switch ( kaigishitsu_number ) {
case CLIP:
sprintf( file , "CLIP\\CLIP.TX%d", count ); break;
case PATIO:
sprintf( file , "PATIO\\%s.PA%d", forum_name , count ); break;
case HP:
sprintf( file , "HP\\%s\\%s.HP%d",forum_name ,forum_name , count );
break;
case MAIL:
case ALL_MAIL:
if ( MakeUpMAILInto1FileSw == YES ) {
sprintf( file , "NIFMAIL\\%s" , ALL_IN_1_MAIL_FILE_NAME );
} else {
sprintf( file , "NIFMAIL\\%s.ML%d", forum_name , count );
};
break;
case BILL: sprintf( file , "BILL\\BILL.BL%d", count );
break;
case COLLECT_ID:
sprintf( file , "COLLECT\\%s.TX%d", forum_name , count );
break;
default:
sprintf(file,"%s\\MES%2d_%2d.TXT",
forum_name,kaigishitsu_number,count
);
break;
};
strcpy( file_name , output_path );
strcat( file_name , file );
file_name_space_0( file_name );
check_and_make_output_path( file_name );
}